package com.ruoyi.common.utils.img;

import cn.hutool.core.img.ImgUtil;
import com.ruoyi.common.config.RuoYiConfig;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;


@Slf4j
public class ImageUtils {

    private static final String
            defaultBaseDir = RuoYiConfig.getProfile(),//默认上传的地址
            JPG = "jpg",
            DEFAULT_FILE_PAHT = defaultBaseDir + "/cert/%s-zs." + JPG,
            ORGANIZATION = "公卫处方权服务管理中心制"
            ;

    // 加载自定义字体的方法
    private static Font loadFont(String fontFileName, int style, int size) {
        try {
            InputStream inputStream = ImageUtils.class.getResourceAsStream("/font/" + fontFileName);
            if (inputStream != null) {
                Font font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
                return font.deriveFont(style, size);
            }
        } catch (Exception e) {
            log.error("加载字体失败: {}", fontFileName, e);
        }
        // 如果加载失败，返回备用字体
        return new Font("SimHei", style, size);
    }

    // 使用自定义字体
    private static final Font
            MICROSOFT_ACCOR_BLACK_350 = loadFont("MSYHBD.TTF", Font.BOLD, 350),
            MICROSOFT_ACCOR_BLACK_500 = loadFont("MSYHBD.TTF", Font.BOLD, 500),
            MICROSOFT_ACCOR_BLACK_400 = loadFont("MSYHBD.TTF", Font.BOLD, 400);
//    private static final Font
//            MICROSOFT_ACCOR_BLACK_350 = new Font("微软雅黑", Font.BOLD, 350),
//            MICROSOFT_ACCOR_BLACK_500 = new Font("微软雅黑", Font.BOLD, 500),
//            MICROSOFT_ACCOR_BLACK_400 = new Font("微软雅黑", Font.BOLD, 400);

    public static final int
            $0 = 0 ,
            $255 = 255 ,
            $1000 = 1000 ,
            $1500 = 1500 ,
            $2000 = 2000 ,
            $2500 = 2500 ,
            $3500 = 3500 ,
            $4000 = 4000 ,
            $4500 = 4500 ,
            $5500 = 5500 ,
            $7200 = 7200,
            $8000 = 8000 ,
            $9500 = 9500 ,
            $10000 = 10000;


    /**
     * 创建证件照
     * @param stuPic 学生头像
     * @param cachet 印章
     * @param background 背景
     * @param name 学生姓名
     * @param job 部门
     * @param title 标题
     * @param type 类型
     * @return 文件地址
    */
    public static String savePic(File stuPic, File cachet,File background,String name,String job, String title,String type,String exp , String postsNumber) throws Exception {
        // 创建一个新的空白图片
        BufferedImage blackImage = new BufferedImage($8000, $10000, BufferedImage.TYPE_INT_ARGB);
        blackImage.setRGB($255,$255,$255);
        //加载模板图片和需要替换的图片数据
        BufferedImage stuBufferedImage = ImgUtil.read(stuPic);
        BufferedImage cachetBufferedImage = ImgUtil.read(cachet);
        BufferedImage backgroundBufferedImage = ImgUtil.read(background);

        Graphics2D graphics = blackImage.createGraphics();//创建图形
        graphics.setBackground(Color.WHITE);
        graphics.fillRect($0,$0,$8000,$10000);
        //添加背景图片
        graphics.drawImage(backgroundBufferedImage,$0,$0,$8000,$10000,null);
        graphics.setFont(MICROSOFT_ACCOR_BLACK_500);
        graphics.setColor(Color.WHITE);
        //标题
        FontMetrics fontMetrics = graphics.getFontMetrics(MICROSOFT_ACCOR_BLACK_500);
        int textWidth = fontMetrics.stringWidth(title);
        int centerX = $8000 / 2 - textWidth / 2;
        graphics.drawString(title, centerX, $1000);
        //证书类型
        graphics.setFont(MICROSOFT_ACCOR_BLACK_400);
        textWidth = fontMetrics.stringWidth(type);
        centerX = $8000 / 2 - textWidth / 2;
        graphics.drawString(type, centerX, $2000);
        centerX = $8000 / 2 - $3500 / 2;
        //学生照片
        graphics.drawImage(stuBufferedImage, centerX, $2500,$3500, $4000, null);
        //姓名及工作单位 及 颜色
        graphics.setColor(Color.BLACK);
        graphics.setFont(MICROSOFT_ACCOR_BLACK_350);
        graphics.drawString("姓名："+name, $2500, $7200);
        graphics.drawString("   工作单位："+job, $1500, $8000); //todo 工作单位
        graphics.setColor(Color.WHITE);
        //尾部说明
        graphics.setFont(MICROSOFT_ACCOR_BLACK_500);
        textWidth = fontMetrics.stringWidth(ORGANIZATION);
        centerX = $8000 / 2 - textWidth / 2;
        graphics.drawString(ORGANIZATION, centerX, $9500);
        //机构公章
        centerX = $8000 / 2 - $5500 / 2;
        graphics.drawImage(cachetBufferedImage, centerX, $5500, $5500, $4500, null);
        // 保存生成的图片
        String fileName = String.format(DEFAULT_FILE_PAHT,name);
        ImgUtil.write(blackImage,new File(fileName));
        //压缩
        //ImgUtil.compress(new File(fileName),new File(fileName),0.3f);
        //Img.from(new File(fileName)).scale(0.3f).write(new File(fileName));
        return fileName;
    }

}
